package com.doubao.device.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.doubao.common.result.Result;
import com.doubao.device.dto.SmartDeviceDTO;
import com.doubao.device.entity.SmartDevice;
import com.doubao.device.mapper.SmartDeviceMapper;
import com.doubao.device.security.DeviceContext;
import com.doubao.device.service.SmartDeviceService;
import com.doubao.device.service.UserDeviceCollectionService;
import com.doubao.user.service.MembershipService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/device/collection")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "智能体收藏管理", description = "用户智能体收藏相关接口")
public class UserDeviceCollectionController {

    private final UserDeviceCollectionService collectionService;
    private final MembershipService membershipService;
    // 添加缺少的依赖
    private final SmartDeviceService deviceService;

    @PostMapping("/{deviceId}")
    @Operation(summary = "收藏智能体", description = "收藏智能体")
    public Result<Void> collectDevice(
            @RequestHeader(value = "X-User-ID", required = false) Long userId,
            @PathVariable Long deviceId) {
        if (userId == null) {
            return Result.failed("用户未登录");
        }

        // 首先查询智能体信息，检查是否存在
        SmartDevice device = deviceService.getById(deviceId);
        if (device == null) {
            return Result.failed("智能体不存在");
        }

        // 检查智能体是否为会员专属
        if (device.getIsPremium() == 1) {
            // 如果是会员智能体，检查用户是否是会员
            Result<Void> membershipResult = membershipService.consumeSwitchCount(userId);
            if (!membershipResult.isSuccess()) {
                return membershipResult; // 如果不是会员，返回错误信息
            }
        }

        // 收藏智能体
        return collectionService.collectDevice(userId, deviceId);
    }

    @DeleteMapping("/{deviceId}")
    @Operation(summary = "取消收藏", description = "取消收藏指定的智能体")
    public Result<Void> cancelCollection(
            @Parameter(description = "智能体ID") @PathVariable Long deviceId) {
        // 从上下文中获取用户ID，而不是从请求头
        Long userId = DeviceContext.getUserId();
        if (userId == null) {
            return Result.failed("用户未登录");
        }
        return collectionService.cancelCollection(userId, deviceId);
    }

    @GetMapping("/check/{deviceId}")
    @Operation(summary = "检查是否已收藏", description = "检查用户是否已收藏指定智能体")
    public Result<Boolean> checkIsCollected(
            @Parameter(description = "智能体ID") @PathVariable Long deviceId) {
        // 从上下文中获取用户ID，而不是从请求头
        Long userId = DeviceContext.getUserId();
        if (userId == null) {
            return Result.failed("用户未登录");
        }
        return collectionService.checkIsCollected(userId, deviceId);
    }

    @GetMapping("/list")
    @Operation(summary = "获取用户收藏列表", description = "获取用户收藏的智能体列表")
    public Result<IPage<SmartDeviceDTO>> getUserCollections(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size) {
        // 从上下文中获取用户ID，而不是从请求头
        Long userId = DeviceContext.getUserId();
        if (userId == null) {
            return Result.failed("用户未登录");
        }
        return collectionService.getUserCollections(userId, page, size);
    }
}